package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.Item.ItemInfoDto;
import com.deer.wms.base.system.model.Item.ItemType;
import com.deer.wms.base.system.model.Item.ItemTypeCriteria;
import com.deer.wms.base.system.model.Item.ItemTypeDto;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.base.system.service.ItemTypeService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by guo on 2019/11/19.
 */
@Api(description = "物料类型api接口")
@RestController
@RequestMapping("/item/types")
public class ItemTypeController {

    @Autowired
    private ItemTypeService itemTypeService;

    @Autowired
    private ItemInfoService itemInfoService;

    @ApiOperation(value = "新增itemType以及itemType列表", notes = "新增itemType以及itemType列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody ItemType itemType, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        String nowDate = DateUtils.getNowDateTimeString();
//        Integer organizationId = itemType.getOrganizationId();
//        String itemTypeCode="IT"+ organizationId + RandomNo.createTimeString().substring(7,14);
//        itemType.setItemTypeCode(itemTypeCode);
        itemType.setCreateTime(nowDate);
        itemTypeService.save(itemType);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除itemType信息", notes = "删除itemType信息")
    public Result delete(Integer itemTypeId) {
        List<ItemInfoDto> list = itemInfoService.itemInfoFindByItemTypeId(itemTypeId);
        if (list.size() == 0) {
            itemTypeService.deleteByIdAndLowLevel(itemTypeId);
        } else {
            throw new ServiceException(CommonCode.ITEM_TYPE_EXISTS, "此物料类型正在使用");
        }
        return ResultGenerator.genSuccessResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新itemType信息", notes = "更新itemType信息")
    public Result update(@RequestBody ItemType itemType) {
        itemTypeService.updateById(itemType);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        ItemType itemType = itemTypeService.getById(id);
        return ResultGenerator.genSuccessResult(itemType);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/list")
    @ApiOperation(value = "物料类型查询列表", notes = "物料类型查询列表")
    public Result list(ItemTypeCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
//        if (currentUser.getOrganizationId() == -1) {
//            criteria.setOrganizationId(-1);
//        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ItemTypeDto> list = itemTypeService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/treeParentList")
    @ApiOperation(value = "treeParentList", notes = "treeParentList")
    public Result list(ItemType itemType) {
        List<ItemTypeDto> itemTypes = itemTypeService.findParentList(itemType);
        List<Integer> list = itemTypeService.treeParentList(itemTypes, itemType);
        return ResultGenerator.genSuccessResult(list);
    }

}
